﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Order_PayDetailEdit : System.Web.UI.Page
{
    private string Code
    {
        get
        {
            return Request.QueryString["code"] != null ? Request.QueryString["code"].ToString() : "";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("400101");
        if (!IsPostBack)
        {
            BindDataList();
            btnCancel.Attributes.Add("onclick", "if(!confirm('确定要取消并返回上一页吗？'))return false;");
        }
    }

    private DataSet DstPayDetails
    {
        get
        {
            DataSet dstPayDetails = null;
            if (ViewState["DstPayDetails"] != null)
            {
                dstPayDetails = (DataSet)ViewState["DstPayDetails"];
            }
            return dstPayDetails;
        }
        set
        {
            ViewState["DstPayDetails"] = value;
        }
    }

    private void BindDataList()
    {
        this.DstPayDetails = new BLL.PAYDETAIL().GetNotGetPayDetailByOrderId(this.Code);
        dlistPayDetail.DataSource = this.DstPayDetails;
        dlistPayDetail.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("400101", "00017"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        try
        {
            StringBuilder strSql = new StringBuilder();
            Label lblDetailId;
            TextBox txtPayAmt;
            Label lblHraAmt;
            Label lblGetAmt;
            int count = 0;
            decimal payAmount = 0.00M;

            foreach (DataListItem tempItem in dlistPayDetail.Items)
            {
                lblDetailId = tempItem.FindControl("lblDetailId") as Label;
                txtPayAmt = tempItem.FindControl("txtPayAmt") as TextBox;
                lblHraAmt = tempItem.FindControl("lblHraAmt") as Label;
                lblGetAmt = tempItem.FindControl("lblGetAmt") as Label;

                if (Convert.ToDecimal(lblGetAmt.Text.Trim()) + Convert.ToDecimal(lblHraAmt.Text) >= Convert.ToDecimal(txtPayAmt.Text))
                {
                    strSql.Append("update paydetail set PD_GET=1,PD_PAYAMT=");
                    strSql.Append(txtPayAmt.Text.Trim());
                    strSql.Append(" where PD_ID='");
                    strSql.Append(this.Code);
                    strSql.Append("' and PD_DETAILID='");
                    strSql.Append(lblDetailId.Text);
                    strSql.Append("'|");

                    payAmount += Convert.ToDecimal(txtPayAmt.Text.Trim());
                    count++;
                }
                else
                {
                    strSql.Append("update paydetail set PD_PAYAMT=");
                    strSql.Append(txtPayAmt.Text.Trim());
                    strSql.Append(" where PD_ID='");
                    strSql.Append(this.Code);
                    strSql.Append("' and PD_DETAILID='");
                    strSql.Append(lblDetailId.Text);
                    strSql.Append("'|");
                }
                
            }

            if (count == this.DstPayDetails.Tables[0].Rows.Count)
            {
                strSql.Append("update orderdoc set ORDER_CLEAR=1,ORDER_CLEARAMT=");
                strSql.Append(payAmount);
                strSql.Append(",BYZD9='");
                strSql.Append(DateTime.Now);
                strSql.Append("' where ORDER_ID='");
                strSql.Append(this.Code);
                strSql.Append("'|");
            }
            else
            {
                strSql.Append("update orderdoc set ORDER_CLEARAMT=");
                strSql.Append(payAmount);
                strSql.Append(" where ORDER_ID='");
                strSql.Append(this.Code);
                strSql.Append("'|");
            }

            string stringSql = strSql.ToString();
            char[] charToTrim = new char[] { '|' };
            string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
            if (new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray))
            {
                Share.InsertLog("400101", "00017");
                Response.Write("<script>alert('支付明细修改成功！');location.href='OrderDetail.aspx?code=" + this.Code + "'</script>");
            }
            else
            {
                Share.Alert(Page, "反审批失败"); return;
            }
        }
        catch (FormatException ex)
        {
            Share.Alert(this.Page,"请输入正确的数据！");
        }
        catch(Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderDetail.aspx?code=" + this.Code);
    }
}
